//封面图片
var coverImgArray = new Array();

/**
 * 回显图片
 */
function appendImageToPage(uploadId, image){
	var url = BASE_PATH + '/' + image.imageUrl;
	var imageDiv = $('<div class="file-preview-frame" id="' + image.imgId + '" data-fileindex="' + image.imgIndex + '"></div>');
	var img = $('<img src="' + url + '" class="file-preview-image" title="' + image.imgName + '" alt="' + image.imgName + '" style="width:100px;height:132px;">');
	var divFooter = $('<div class="file-thumbnail-footer"></div>');
	var footerNameDiv = $('<div class="file-caption-name" title="' + image.imgName + '" style="width: 120px;">' + image.imgName + '</div>');
	var footerActionsDiv = $('<div class="file-actions"></div>');
	var footerButtonDiv = $('<div class="file-footer-buttons"></div>');
	var downloadButton = $('<a class="kv-file-remove btn btn-xs btn-default" style="margin-right: 10px;" href="javascript:void(0)"  title="下载图片" onclick="downloadImages(\''+uploadId+'\')"> 下载</a>');
	var showButton = $('<a class="kv-file-remove btn btn-xs btn-default" style="margin-right: 15px;"  href="javascript:void(0)" title="预览图片" onclick="showImage(\''+url+'\',\''+image.imgId+'\')"> 预览</a>');
	var fixDiv = $('<div class="clearfix"></div>');
	downloadButton.appendTo(footerButtonDiv);
	showButton.appendTo(footerButtonDiv);
	footerButtonDiv.appendTo(footerActionsDiv);
	fixDiv.appendTo(footerActionsDiv);
	footerNameDiv.appendTo(divFooter);
	footerActionsDiv.appendTo(divFooter);
	img.appendTo(imageDiv);
	divFooter.appendTo(imageDiv);
	$('#' + uploadId + 'ImagesDiv').append(imageDiv);
	$('#' + uploadId + 'FilePreview').show();
	$('#download' + uploadId).show();
}

/**
 * 预览图片
 * @param imageUrl
 * @param imgId
 */
function showImage(imageUrl,imgId){
	MainTab.createTab(imgId, '图片预览', BASE_PATH + '/admin/wsProductController/showProductImage?imageUrl='+imageUrl); //打开一个新页面
}

/**
 * 下载图片
 * @param uploadId
 */
function downloadImages(uploadId){
	if(uploadId == 'cover'){
		imageUrl = getImagesUrl(coverImgArray);
	}
	
	window.location.href = BASE_PATH + '/admin/wsProductController/downloadImages?imageUrl=' + imageUrl;
}

/**
 * 获得图片地址
 * @param imageList
 * @returns
 */
function getImagesUrl(imageList){
	var imageUrl = '';
	for(var i = 0; i < imageList.length; i++){
		if(imageList[i].imageUrl != ''){
			imageUrl += imageList[i].imageUrl + ',';
		}
	}
	return imageUrl.substring(0, imageUrl.length - 1);
}

//修改
function updateProduct(brandId){
	
	window.location.href =  BASE_PATH + "/admin/wsBrandController/toUpdate/"+brandId;
}
var index = 0;
/**
 * 新增
 */
function toAdd(){
	index++;
	var url = BASE_PATH+'/admin/wsBrandController/toSave'
	MainTab.createTab('ws_brand_add_'+index, '新增关联品牌', url); //打开一个新页面
}            

/**
 * 删除
 * @param productId
 */
function deleteProduct(brandId){
	$.plugin.plugLoading("正在删除，请稍后...");
	var url = BASE_PATH + "/admin/wsBrandController/deleteProduct";
	$.ajax({
		type: 'POST',
		url: url, 
		data: {"brandId":brandId}, 
		dataType: 'json',
		success: function(data) {
			if(data.success){
				$.plugin.close();
				MainTab.closeTabAndCallChiledFunction('关联品牌', 'messagerAlert()');
			}else{
				$.plugin.close();
				$.messager.alert("提示",data.msg,"info");
			}
		}
	});
}
